package com.ytktt.study.week3.task2;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpMessage;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

/**
 * TODO
 *
 * @author ytk
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter {

    private String result;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            FullHttpMessage fullHttpMessage = (FullHttpMessage) msg;
            result = fullHttpMessage.content().toString(CharsetUtil.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ReferenceCountUtil.release(msg);
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    public String getResult() {
        return this.result;
    }
}
